<?php
// src/Pyaf/GymBundle/Controller/AlumnoController.php
namespace Pyaf\GymBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Pyaf\GymBundle\Entity\Cuota;
use Pyaf\GymBundle\Entity\Alumno;

/**
 * Description of ProfesorController
 *
 * @author Toshiba P755-S5272
 */
class CuotaController extends Controller{

    public function listAction(){
        $em = $this->getDoctrine()->getEntityManager();
        
        try {
            
            $alumnosDeben = $em->getRepository('PyafGymBundle:Cuota')->findAllAlumnosDeben();
            $alumnosNoDeben = $em->getRepository('PyafGymBundle:Cuota')->findAllAlumnosNoDeben();

            $alumnos = $this->JuntarAlumnos($alumnosDeben, $alumnosNoDeben);

            return $this->render('PyafGymBundle:Cuota:list.html.twig', array('alumnos' => $alumnos));
        } catch (Exception $exc) {
            throw new NotFoundHttpException('The product does not exist.');
        }

    }

    public function pagoAction(){
        $em = $this->getDoctrine()->getEntityManager();
        $peticion = $this->getRequest();
        $id_alumno = (Int)$peticion->request->get('id_alumno');
        $alumno = $em->getRepository('PyafGymBundle:Alumno')->findAlumno($id_alumno);
        $cuotas = $em->getRepository('PyafGymBundle:Cuota')->findAllByAlumno($alumno->getId());
                
        return $this->render('PyafGymBundle:Cuota:pagar.html.twig', array('alumno' => $alumno, 'cuotas' => $cuotas));
    }
    public function debenAction(){
    }
    
    public function pagosAlumnoAction(){
        $em = $this->getDoctrine()->getEntityManager();
        $peticion = $this->getRequest();
        $id_alumno = (Int)$peticion->request->get('id_alumno');
        $alumno = $em->getRepository('PyafGymBundle:Alumno')->findAlumno($id_alumno);
        $cuotas = $alumno->getCuotas();
        
        return $this->render('PyafGymBundle:Cuota:show.html.twig', array('alumno' => $alumno, 'cuotas' => $cuotas));
    }
    
    public function registrarPagoAction($id){
        $em = $this->getDoctrine()->getEntityManager();
        $peticion = $this->getRequest();
        try {
            $alumno = $em->getRepository('PyafGymBundle:Alumno')->findAlumno($id);
            $ids_cuotas = $peticion->request->get('cuotaPagaCobro');
            $cuotaFechaCobro = $peticion->request->get('cuotaFechaCobro');
            $cuotaMontoCobro = $peticion->request->get('cuotaMontoCobro');
            $cuota = new Cuota();
            foreach ($ids_cuotas as $cuotaId) {
                $cuota = $em->getRepository('PyafGymBundle:Cuota')->find($cuotaId);
                $cuota->setAlumno($alumno);
                $cuota->setFechaAbono($cuotaFechaCobro);
                $cuota->setMonto($alumno->getCuota());
                $cuota->setEstado(true);
                $em->persist($cuota);
                $em->flush();
            }
            $this->get('session')
                 ->setFlash(
                               'alertaCuotaSuccess',
                               'El cobro de la cuota ha sido registrada satisfactoriamente!'
                           );
            
            return $this->redirect($this->generateUrl('cuotas_list'));
        } catch (Exception $exc) {
            $this->get('session')
                 ->setFlash(
                               'alertaCuotaError',
                               'Error, ha ocurrido un problema al registrar la transacción, intente nuevamente!'
                           );
            
            return $this->redirect($this->generateUrl('cuotas_list'));
        }
    }
    
    public function editAction($id){
    }
    
    public function aumentoAction() {
        $peticion = $this->getRequest();
        $aumento = $peticion->request->get('aumentoCuota');
        $em = $this->getDoctrine()->getEntityManager();
        $alumnos = $em->getRepository('PyafGymBundle:Alumno')->findAllOrderByNombre();
        try {
            foreach ($alumnos as $alumno) {
                $alumno->setCuota($alumno->getCuota() + ($alumno->getCuota() * ($aumento / 100)));
                $em->persist($alumno);
                $em->flush();
            }
            
            $this->get('session')
                 ->setFlash(
                               'alertaCuotaSuccess',
                               "El aumento del ($aumento %) se ha aplicado con Éxito!"
                           );
        } catch (Exception $exc) {
            $this->get('session')
                 ->setFlash(
                               'alertaCuotaError',
                               "El aumento del ($aumento %) se ha ha sufrido algún tipo de error, vuelva a intentar!"
                           );
        }

        
        return $this->redirect($this->generateUrl('cuotas_list'));
    }
    /*
     * ************************************************************************
     */
    
    private function JuntarAlumnos($a1, $a2) {
        $alumnos = array();
        
        foreach ($a1 as $value) {
            array_push($alumnos, $value);
        }
        foreach ($a2 as $value) {
            array_push($alumnos, $value);
        }
        
        return $alumnos;
    }
}

?>
